package com.baizhi.controller;


import com.baizhi.entity.Menu;
import com.baizhi.entity.RoleMenuDto;
import com.baizhi.entity.RoleResource;
import com.baizhi.service.IRoleResourceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author haiyu
 * @since 2021-09-06
 */
@RestController
@RequestMapping("/roleResource")
public class RoleResourceController {

    @Autowired
    private IRoleResourceService rrs;

    @PostMapping("/addRoleResource")
    public void addRoleResource(@RequestBody RoleMenuDto dto){
        //先从数据库里面把该角色之前的权限删除调。
        QueryWrapper<RoleResource> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",dto.getRoleId());
        rrs.remove(wrapper);


        List<Integer> menuIds = dto.getMenuIds();
        for (Integer menuId:menuIds){
            RoleResource rr = new RoleResource();
            rr.setRoleId(dto.getRoleId());
            rr.setResourceId(menuId);
            rrs.save(rr);
        }
    }
    //根据角色的id获取到角色对应的所有的权限
    @GetMapping("/selectDuiyqx/{roleId}")
    @PreAuthorize("hasAuthority('sys:schedule:list,sys:schedule:info')")
    public List<RoleResource> selectDyqx(@PathVariable("roleId") Integer roleId){
        System.out.println("roleIsssssssssssssssssssssssssssssssssssd = " + roleId);
        QueryWrapper<RoleResource> qw = new QueryWrapper<>();
        qw.eq("role_id",roleId);
        List<RoleResource> list = rrs.list(qw);
        list.forEach(System.out::println);
        return list;
    }

}

